/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.gui;

import com.sleekice.core.GameSpecs;
import com.sleekice.core.Renderable;
import com.sleekice.core.Updatable;
import com.sleekice.util.GL;
import java.awt.Graphics2D;

public class MessageDialog implements Renderable, Updatable,
    Widget {

    /**
     * Type of messages:
     * ABORT: quit after button click
     * CONTINUE: remove message after button click
     */
    public static final int ABORT = 0;
    public static final int CONTINUE = 1;
    private TextArea textArea_;
    private TextButton okButton_;
    private int x_;
    private int y_;
    private int width_;
    private int height_;
    private GuiHandler guiHandler_;
    private int type_;

    public MessageDialog(String message, int x, int y, int width, int height,
        int type) {
        x_ = x;
        y_ = y;
        width_ = width;
        height_ = height;
        type_ = type;

        textArea_ =
            new TextArea(x_ + 10, y_ + 10, width_ - 20, height_ - 50);
        textArea_.addText(message);
        if (type_ == CONTINUE) {
            okButton_ =
                new TextButton("OK", x_ + width_ / 2 - 60, y_ + height_ - 30);
        }
        else if (type_ == ABORT) {
            okButton_ =
                new TextButton("ABORT", x_ + width_ / 2 - 60, y_ + height_ - 30);
        }
        okButton_.setBobbleOnMouseOver(false);
        okButton_.resize(120, 23);
        okButton_.centerText();
    }

    public static MessageDialog getCentered(GameSpecs specs,
        String message, int width, int height, int type) {
        return new MessageDialog(message, specs.getScreenWidth() / 2 - width / 2,
            specs.getScreenHeight() / 2 - height / 2, width, height,type);
    }

    public TextButton getOkButton() {
        return okButton_;
    }

    public boolean isEnabled() {
        return true;
    }

    public boolean isVisible() {
        return true;
    }

    public void setEnabled(boolean enabled) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setGuiHandler(GuiHandler guiHandler) {
        guiHandler_ = guiHandler;
    }

    public void setVisible(boolean visible) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(double elapsedTime) {
        okButton_.update(elapsedTime);

        /**
         * Once the button is clicked, remove this message dialog from the GUI
         * Handler and also remove the button from the mouse input.
         */
        if (okButton_.isClicked()) {
            if (guiHandler_ != null) {
                if (type_ == CONTINUE) {
                    guiHandler_.removeWidget(this);
                }
                else if (type_ == ABORT) {
                    guiHandler_.getMain().stop();
                }
            }
        }
    }

    public void render(Graphics2D g) {
        g.setColor(GL.blueRoyal);
        g.fillRect(x_, y_, width_, height_);

        textArea_.render(g);
        okButton_.render(g);
    }
}